<template>
    <view class="img-cell">
        <view class="">
            <view class="add-img" @tap="chooseImage">
                <uni-icons type="camera" size="80"></uni-icons>
                <text>添加图片</text>
            </view>
        </view>
        <view class="img-item" v-for="(image, index) in imageListShow" :key="index">
            <image
                class="img"
                mode="aspectFill"
                :src="easyGetImgUrl(image)"
                :data-src="easyGetImgUrl(image)"
                @tap="previewImage(index)">
            </image>
            <view @click="del(index)">
                <uni-icons class="btn-del" type="closeempty" customIcons="icon-white"></uni-icons>
            </view>
        </view>
		<!-- <privacy-agreement></privacy-agreement> -->
    </view>
</template>

<script>
	// import privacyAgreement from "@/components/privacy-agreement/privacy-agreement.vue"
import { uploadImage } from "@/uni-sub-base/service/api/file.js"
import permision from "@/utils/permission.js"
export default {
    props: {
        imageList: {
            type: Array,
            default: () => [],
        },
    },
		components: {
			// privacyAgreement
		},
    data() {
        return {
            imageListShow: [],
        }
    },
    mounted() {
        this.imageListShow = JSON.parse(JSON.stringify(this.imageList))
    },
    methods: {
        //选择图片
		//#ifdef APP
		async chooseImage() {
			const result = await permision.premissionCheck("EXTERNAL_STORAGE")
			if(result == 1) {  
			    // 此处处理各自业务 如：uni.chooseImage  
				let that = this
				uni.chooseImage({
				    count: 1, //默认9
				    sizeType: ["original", "compressed"], //可以指定是原图还是压缩图，默认二者都有
				    sourceType: ["album","camera"], //从相册选择
				    success: function (res) {
				        that.upLoadPicFn(res)
				    },
				})
			}
		},
		//#endif
		// #ifndef APP
        chooseImage() {
            let that = this
            uni.chooseImage({
                count: 1, //默认9
                sizeType: ["original", "compressed"], //可以指定是原图还是压缩图，默认二者都有
                sourceType: ["album","camera"], //从相册选择
                success: function (res) {
                    that.upLoadPicFn(res)
                },
            })
        },
		//#endif
        //删除图片
        del(index) {
            this.imageList.splice(index, 1)
            this.imageListShow.splice(index, 1)
            this.$emit("del", this.imageList)
        },
        //上传图片
        upLoadPicFn(files) {
            let param = {
                filePath: files.tempFilePaths[0],
                formData: {},
            }
            uploadImage(param).then((e) => {
                if (e.Data) {
                    this.imageList.push(e.Data.Path)
                    this.imageListShow.push(e.Data.FullPath)
                    this.$emit("chooseImage", this.imageList)
                }
            })
        },
        //预览图片
        previewImage(index) {
            var current = index
            uni.previewImage({
                current: current,
                urls: this.imageListShow,
            })
        },
    },
}
</script>

<style lang="scss"></style>
